package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ExportQuarantineResultDetailItem;
import egovframework.npqs.pqi.mobile.jsoncontent.QuarantineResultDetailItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.BodySeListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.QuarantineDetailListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.QunCdItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.ResultFlagListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.TypeListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.ValidFlagListItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class ExportQuarantineResultDetailThread extends BasicThread{
	public interface ExportQuarantineResultDetailThreadCallback {
		public void OnExportQuarantineDetailCompleted(boolean isNetworkConntected
				, ExportQuarantineResultDetailItem exportItem, String message);
		public void OnExportQuarantineDetailError(ErrorItem errorItem);
	}

	private String url;
	private ExportQuarantineResultDetailThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String offCd;
	private String recNo;

	public ExportQuarantineResultDetailThread(Context context
			, ExportQuarantineResultDetailThreadCallback callback
			, String offCd, String recNo, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.offCd = offCd;
		this.recNo = recNo;
		this.appStatus = (AppStatus) context.getApplicationContext();
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String param = "";
		
		map.put(Const.SEARCHOFFCD, offCd);
		map.put(Const.SEARCHRECNO, recNo);
		param = parameterReturn(Const.REQ043, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnExportQuarantineDetailCompleted(false, null, "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnExportQuarantineDetailError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				callback.OnExportQuarantineDetailError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayQuarantine = json.getJSONArray(Const.DATA);
				JSONObject quarantine = arrayQuarantine.getJSONObject(0);
				
				ExportQuarantineResultDetailItem exportItem = new ExportQuarantineResultDetailItem();
				
				//lmoList 
				ArrayList<BodySeListItem> arrayLmo = new ArrayList<BodySeListItem>();
				JSONArray lmoItems = json.getJSONArray(ExportQuarantineResultDetailItem.LMOLIST);
				for(int i=0; i <lmoItems.length(); i++){
					JSONObject itemObject = lmoItems.getJSONObject(i);
					BodySeListItem item = new BodySeListItem();
					item.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, BodySeListItem.CODENM)));
					item.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, BodySeListItem.CODE)));
					arrayLmo.add(item);
				}
				exportItem.setLmoList(arrayLmo);
				
				//검역결과코드리스트 : RESULTFLAGLIST
				ArrayList<ResultFlagListItem> arrayVal = new ArrayList<ResultFlagListItem>();
				JSONArray valitems	 	= json.getJSONArray(ExportQuarantineResultDetailItem.RESULTFLAGLIST);
				for(int i = 0; i < valitems.length(); i++) {
					JSONObject itemObject = valitems.getJSONObject(i);
					ResultFlagListItem Item = new ResultFlagListItem();
					Item.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ValidFlagListItem.CODE)));
					Item.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ValidFlagListItem.CODENM)));
					arrayVal.add(Item);
				}
				exportItem.setResultFlagList(arrayVal);
				
				//품목정보리스트 : QuarantineDetailListItem
				ArrayList<QuarantineDetailListItem> arrQuarantine = new ArrayList<QuarantineDetailListItem>();
				JSONArray quaItems = json.getJSONArray(ExportQuarantineResultDetailItem.ITEMS);
				for(int i = 0; i < quaItems.length(); i++) {
					JSONObject itemObject = quaItems.getJSONObject(i);
					QuarantineDetailListItem quaItem = new QuarantineDetailListItem();
					quaItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.OFFCD)));
					quaItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.RECNO)));
					quaItem.setRecSeq(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.RECSEQ)));
					quaItem.setAftDsuseTrgetCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.AFTDSUSETRGETCD)));
					quaItem.setDbyhsProcessCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.DBYHSPROCESSCD)));
					quaItem.setPrdlstCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.PRDLSTCODE)));
					quaItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.PRDLSTNM)));
					quaItem.setItmQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.ITMQTY)));
					quaItem.setInsQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.INSQTY)));
					quaItem.setQunCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.QUNCD)));
					quaItem.setLmoAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.LMOAT)));
					quaItem.setCntanrLnlAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.CNTANRLNLAT)));
					quaItem.setIslctvtAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.ISLCTVTAT)));
					quaItem.setQrantExclAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.QRANTEXCLAT)));
					quaItem.setPieximrAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.PIEXIMRAT)));
					quaItem.setResultFlag(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.RESULTFLAG)));
					quaItem.setItemType(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.ITEMTYPE)));
					quaItem.setCountryYn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.COUNTRYN)));
					quaItem.setLmoResult(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.LMORESULT)));
					quaItem.setLmoResultYn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarantineDetailListItem.LMORESULTYN)));
					arrQuarantine.add(quaItem);
				}
				exportItem.setItems(arrQuarantine);
				
				//수량단위리스트 : QunCdItem
				ArrayList<QunCdItem> arrayQun = new ArrayList<QunCdItem>();
				JSONArray qunitems	 	= json.getJSONArray(ExportQuarantineResultDetailItem.QUNCDLIST);
				for(int i = 0; i < qunitems.length(); i++) {
					JSONObject itemObject = qunitems.getJSONObject(i);
					QunCdItem qunItem = new QunCdItem();
					qunItem.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QunCdItem.CODE)));
					qunItem.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QunCdItem.CODENM)));
					arrayQun.add(qunItem);
				}
				exportItem.setQunCdList(arrayQun);
				//나머지
				exportItem.setItemsResult(json.getString(ExportQuarantineResultDetailItem.ITEMSRESULT));
				
				exportItem.setResOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.RESOFFCD)));
				exportItem.setResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.RESNO)));
				exportItem.setRegType(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.REGTYPE)));
				exportItem.setMsgId(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.MSGID)));
				exportItem.setTsqno(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.TSQNO)));
				exportItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.OFFCD)));
				exportItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.RECNO)));
				exportItem.setNatCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.NATCD)));
				exportItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.NATNM)));
				exportItem.setVenNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.VENNM)));
				exportItem.setVenRep(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.VENREP)));
				exportItem.setVenTelNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.VENTELNO)));
				exportItem.setDelayNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.DELAYNM)));
				exportItem.setDelayRcognAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.DELAYRCOGNAT)));
				exportItem.setResultFlag(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.RESULTFLAG)));
				exportItem.setResultProgrsSt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.RESULTPROGRSST)));
				exportItem.setResultProgrsNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.RESULTPROGRSNM)));
				exportItem.setEndDivYn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.ENDDIVYN)));
				exportItem.setResDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.RESDE)));
				exportItem.setFieDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.FIEDE)));
				exportItem.setInsPlace(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.INSPLACE)));
				exportItem.setDbyhsCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.DBYHSCNT)));
				exportItem.setdMark(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.DMARK)));
				exportItem.setRemCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.REMCD)));
				exportItem.setRemNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.REMNM)));
				exportItem.setRemark(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.REMARK)));
				exportItem.setImgResult1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.IMGRESULT1)));
				exportItem.setImgResult2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.IMGRESULT2)));
				exportItem.setFileId(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.FILEID)));
				exportItem.setQuranNm1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.QURANNM1)));
				exportItem.setQuranNm2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.QURANNM2)));
				exportItem.setQuranCd1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.QURANCD1)));
				exportItem.setQuranCd2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.QURANCD2)));
				exportItem.setTrnNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.TRNNM)));
				exportItem.setTrnCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, ExportQuarantineResultDetailItem.TRNCD)));
				callback.OnExportQuarantineDetailCompleted(true, exportItem, mResult);
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}
}
